home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / ABUSESRC.ZIP / AbuseSrc / macabuse / imlib / port / x11 / jwindow.c < prev    next >
C/C++ Source or Header  |  1997-05-20  |  11KB  |  464 lines

  1. #include "video.hpp"
  2. #include "image.hpp"
  3. #include "event.hpp"
  4. #include "filter.hpp"
  5. #include "event.hpp"
  6. #include "jwindow.hpp"
  7. #include <X11/Xlib.h>
  8. #include <X11/Xutil.h>
  9. #include <X11/keysym.h>
  10. #include <X11/X.h>
  11.  
  12. int jw_left=5,jw_right=5,jw_top=15,jw_bottom=5;
  13.  
  14. int frame_top() { return 0; }
  15. int frame_bottom() { return 0; }
  16. int frame_left() { return 0; }
  17. int frame_right() { return 0; }
  18.  
  19. extern void update_dirty_window(Window win, image *im, int xoff, int yoff);
  20. extern Window root,mainwin;
  21. extern Display *display;
  22. extern int screen_num;
  23.  
  24. struct jxwin
  25. {
  26.   Window win;  
  27. } ;
  28.  
  29. void set_frame_size(int x)
  30. {  
  31.   return ;
  32. }
  33.  
  34. void window_manager::close_window(jwindow *j)
  35. {
  36.   if (j==first)
  37.     first=first->next;
  38.   else
  39.   {
  40.     for (jwindow *k=first;k->next!=j;k=k->next);
  41.     k->next=j->next;
  42.   }
  43.   XFlush(display);
  44.   delete j;
  45. }
  46.  
  47. void window_manager::hide_windows()
  48. {
  49.   for (jwindow *p=first;p;p=p->next)
  50.     p->hidden=1;
  51. }
  52.  
  53. void window_manager::show_windows()
  54. {
  55.   for (jwindow *p=first;p;p=p->next)
  56.     p->hidden=0;
  57.  
  58. }
  59.  
  60. void window_manager::hide_window(jwindow *j)
  61. {
  62.   j->hidden=1;
  63. }
  64.  
  65. void window_manager::show_window(jwindow *j)
  66. {
  67.   j->hidden=0;
  68. }
  69.  
  70. void window_manager::get_event(event &ev)
  71. {
  72.   jwindow *j;
  73.   eh->get_event(ev);
  74.   if (ev.type==EV_KEY)
  75.     key_state[ev.key]=1;
  76.   else if (ev.type==EV_KEYRELEASE)
  77.     key_state[ev.key]=0;
  78.  
  79.   if (state==inputing)
  80.   {
  81.     int f=0;
  82.     for (j=first;j;j=j->next)
  83.       if (!j->hidden && ((Window)ev.window)==((jxwin *)j->local_info)->win)
  84.       { f=1;
  85.         ev.window=j;
  86.       }
  87.     if (!f) ev.window=NULL;
  88.     
  89.     if (ev.window)
  90.     {
  91.       int closew=0,movew=0;
  92.       if (ev.type==EV_KEY && ev.key==JK_ESC)
  93.         closew=1;
  94.     
  95.       if (closew)
  96.         ev.type=EV_CLOSE_WINDOW;
  97.       else if (ev.window)
  98.         ev.window->inm->handle_event(ev,0,0);
  99.     }
  100.   }
  101.  
  102.   if (ev.type==EV_REDRAW)
  103.   {
  104.     if (ev.window)
  105.       ev.window->screen->add_dirty(ev.redraw.x1,ev.redraw.y1,ev.redraw.x2,ev.redraw.y2);
  106.     else
  107.       screen->add_dirty(ev.redraw.x1,ev.redraw.y1,ev.redraw.x2,ev.redraw.y2);
  108.     flush_screen();
  109.     ev.type=EV_SPURIOUS;   // we took care of this one by ourselves.
  110.   }
  111. }
  112.  
  113. void jwindow::resize(int L, int H)
  114. {
  115.   XWindowChanges v;
  116.   v.width=L;
  117.   v.height=H;
  118.   XFlush(display);
  119.   XConfigureWindow(display,((jxwin *)local_info)->win,CWWidth|CWHeight,&v);
  120.   l=L; h=H;
  121. }
  122.  
  123. void window_manager::resize_window(jwindow *j, int l, int h)
  124. {
  125.   jwindow *p;
  126.   j->resize(l,h);
  127.   j->redraw(hi,med,low,font());
  128. }
  129.  
  130. void window_manager::move_window(jwindow *j, int x, int y)
  131. {
  132.   return ;
  133. }
  134.  
  135. window_manager::window_manager(image *Screen, palette *Pal, int Hi, 
  136.                                int Med, int Low, JCFont *Font)
  137. {
  138.   screen=Screen; hi=Hi; low=Low; med=Med; first=NULL; pal=Pal;
  139.   bk=pal->find_closest(0,0,0);
  140.   state=inputing; fnt=Font; wframe_fnt=Font;
  141.   memset(key_state,0,sizeof(key_state));
  142.   eh=new event_handler(screen,pal);
  143. }
  144.  
  145. jwindow *window_manager::new_window(int x, int y, int l, int h, ifield *fields, char *Name)
  146. {
  147.   if (x>screen->width()-4) x=screen->width()-4;
  148.   if (y>screen->height()-4) y=screen->height()-4;
  149.   
  150.   jwindow *j=new jwindow(x,y,l,h,this,fields,Name),*k;
  151.   j->hidden=0;
  152.   if (!first)
  153.     first=j;
  154.   else
  155.   {
  156.     k=first;
  157.     while (k->next) k=k->next;
  158.     k->next=j;
  159.     j->next=NULL;
  160.   }
  161.   j->redraw(hi,med,low,font());
  162.   return j;
  163. }
  164.  
  165. void window_manager::flush_screen()
  166. {
  167.   jwindow *p,*q;
  168.  
  169.   int mx,my,but;
  170.   image *mouse_pic,*mouse_save;
  171.   
  172.   update_dirty(screen);
  173.  
  174.   for (p=first;p;p=p->next)
  175.   {
  176.     if (!p->hidden)
  177.       update_dirty_window(((jxwin *)p->local_info)->win,p->screen,0,0);
  178.   }
  179. }
  180.  
  181.  
  182. jwindow::jwindow(int X, int Y, int L, int H, window_manager *wm, ifield *fields, char *Name)
  183. {
  184.   ifield *i;
  185.   int x1,y1,x2,y2;
  186.   l=0; h=0; 
  187.   if (fields)
  188.     for (i=fields;i;i=i->next)
  189.     {
  190.       i->area(x1,y1,x2,y2,wm);
  191.       if ((int)y2+1>(int)h) 
  192.         h=y2+1;
  193.       if ((int)x2+1>(int)l) 
  194.         l=x2+1;
  195.     }
  196.   else { l=2; h=2; }
  197.  
  198.   if (L<=0) { l=l-L; } else l=L;
  199.   if (H<=0) { h=h-H; } else h=H;
  200.  
  201.  if (Y<0) y=yres-h+Y-WINDOW_FRAME_TOP-WINDOW_FRAME_BOTTOM-1; else y=Y;
  202.  if (X<0) x=xres-l+X-WINDOW_FRAME_LEFT-WINDOW_FRAME_RIGHT-1; else x=X;
  203.  
  204.   backg=wm->medium_color();
  205.   l+=WINDOW_FRAME_RIGHT; h+=WINDOW_FRAME_BOTTOM;
  206.   if (!fields) { l+=WINDOW_FRAME_LEFT; h+=WINDOW_FRAME_TOP; }
  207.  
  208.   if (l<18) l=18;
  209.   if (h<12) h=12;
  210.   screen=new image(l,h,NULL,2);
  211.   l=screen->width();
  212.   h=screen->height();
  213.   screen->clear(backg);
  214.  
  215.   next=NULL;
  216.   inm=new input_manager(screen,wm,fields);
  217.   if (Name==NULL)
  218.     name=strcpy((char *)jmalloc(strlen(" ")+1,"jwindow::window name")," ");  
  219.   else
  220.     name=strcpy((char *)jmalloc(strlen(Name)+1,"jwindow::window name"),Name);
  221.   local_info=(void *)jmalloc(sizeof(jxwin),"Xwindow struct");
  222.  
  223.   XWindowAttributes wa;
  224.   XGetWindowAttributes(display,mainwin,&wa);
  225.   Window w=XCreateSimpleWindow(display,
  226.                               root,
  227.                               wa.x+x,wa.y+y,
  228.                               l,h,
  229.                               5,
  230.                           BlackPixel(display,screen_num),
  231.                               WhitePixel(display,screen_num)); 
  232.   ((jxwin *)local_info)->win=w;
  233.  
  234.   XSelectInput(display,w,
  235.     KeyPressMask | VisibilityChangeMask | ButtonPressMask | ButtonReleaseMask |
  236.     ButtonMotionMask | PointerMotionMask | KeyReleaseMask |
  237.     ExposureMask | StructureNotifyMask);
  238.  
  239.   XSetTransientForHint(display,w,mainwin);
  240.   XSetWindowColormap(display,w,wa.colormap);
  241.  
  242.  
  243.   XTextProperty xtext;
  244.   ERROR(XStringListToTextProperty(&Name,1,&xtext),"X alloc failed"); 
  245.  
  246.  
  247.   XSizeHints *xsize;
  248.   ERROR((xsize=XAllocSizeHints()),"X alloc failed");
  249.   xsize->flags=PPosition | PSize | PMinSize | PMaxSize;
  250.   xsize->min_width=l;
  251.   xsize->min_height=h;
  252.   xsize->max_width=l;
  253.   xsize->max_height=h;
  254.  
  255.  
  256.   XWMHints *wm_hints;
  257.   ERROR((wm_hints=XAllocWMHints()),"X alloc failed");
  258.   wm_hints->initial_state=NormalState;  // not iconified at first
  259.   wm_hints->input=1;                  // needs keyboard input
  260.   wm_hints->flags=StateHint | InputHint;
  261.  
  262.   
  263.   XSetWMProperties(display,w,&xtext,&xtext,&Name,0,xsize,wm_hints,NULL);
  264.   XFree(xtext.value);
  265.   XFree(xsize);
  266.   XFree(wm_hints);
  267.  
  268.  
  269.  
  270.   XEvent report;
  271.   XMapWindow(display,((jxwin *)local_info)->win);
  272.   do
  273.   { XNextEvent(display, &report);
  274.   } while (report.type!= Expose);     // wait for our window to pop up
  275.   x=y=0;
  276. }
  277.  
  278. void jwindow::local_close()
  279. {
  280.   XDestroyWindow(display,((jxwin *)local_info)->win);
  281.   jfree(local_info);
  282. }
  283.  
  284. void jwindow::redraw(int hi, int med, int low, JCFont *fnt)
  285. {
  286.   if (jw_right>=3)
  287.     screen->rectangle(0,0,l-3,h-3,low);
  288.   if (jw_right>=2)
  289.     screen->rectangle(1,1,l-2,h-2,med);
  290.   if (jw_right>=1)
  291.     screen->rectangle(2,2,l-1,h-1,hi);
  292.  
  293.  
  294.   
  295.   screen->wiget_bar(0,0,l-1,8,hi,med,low);
  296.   screen->line(1,1,l-2,1,low);
  297.   screen->line(1,3,l-2,3,low);
  298.   screen->line(1,5,l-2,5,low);
  299.   screen->line(1,7,l-2,7,low);
  300.  
  301.   screen->wiget_bar(4,3,10,5,hi,med,low);
  302.   screen->rectangle(3,2,11,6,0);  
  303.  
  304.   screen->line(0,8,l-1,8,0);
  305.   if (jw_right>=1)
  306.     screen->wiget_bar(0,9,l-1,h-1,hi,med,low);  
  307.     screen->wiget_bar(0,9,l-1,h-1,hi,med,low);
  308.   if (jw_right>=2)
  309.     screen->wiget_bar(4,13,l-jw_right,h-jw_right,low,med,hi);
  310.  
  311.  
  312.   if (name && name[0])
  313.   {
  314.     short cx1,cy1,cx2,cy2;
  315.     screen->get_clip(cx1,cy1,cx2,cy2);
  316.     screen->set_clip(14,1,l-2,WINDOW_FRAME_TOP-8);
  317.     screen->bar(14,1,14+fnt->width()*strlen(name),WINDOW_FRAME_TOP-8,med);
  318.     fnt->put_string(screen,14,1,name,low);  
  319.     screen->set_clip(cx1,cy1,cx2,cy2);
  320.   }
  321.   
  322.   screen->bar(x1(),y1(),x2(),y2(),backg);
  323.   inm->redraw();
  324. }
  325.  
  326.  
  327. ifield *input_manager::unlink(int id)     // unlinks ID from fields list and return the pointer to it
  328.   for (ifield *i=first,*last;i;i=i->next)
  329.   {
  330.     if (i->id==id) 
  331.     {
  332.       if (i==first)
  333.     first=first->next;
  334.       else
  335.         last->next=i->next;
  336.       if (active==i)
  337.         active=first;
  338.       return i;
  339.     }
  340.     last=i;
  341.   }
  342.   return NULL;   // no such id
  343. }
  344.  
  345. input_manager::~input_manager() 
  346. { ifield *i; 
  347.   while (first) 
  348.   { i=first; 
  349.     first=first->next; 
  350.     delete i; 
  351.   } 
  352.  
  353. void input_manager::handle_event(event &ev, int xoff, int yoff)
  354. {
  355.   ifield *i,*in_area=NULL;
  356.   int x1,y1,x2,y2;
  357.   ev.mouse_move.x-=xoff;
  358.   ev.mouse_move.y-=yoff;
  359.  
  360.   if (ev.type==EV_MOUSE_BUTTON && ev.mouse_button==1)
  361.   {
  362.     for (i=first;i;i=i->next)
  363.     {
  364.       i->area(x1,y1,x2,y2,wm);
  365.       if (ev.mouse_move.x>=x1 && ev.mouse_move.y>=y1 &&
  366.           ev.mouse_move.x<=x2 && ev.mouse_move.y<=y2)
  367.         in_area=i;
  368.     }
  369.     if (in_area!=active && in_area  && in_area->selectable())
  370.     {
  371.       if (active)
  372.         active->draw(0,screen,wm);
  373.       active=in_area; 
  374.       active->draw(1,screen,wm);
  375.     }
  376.   } 
  377.   if (ev.type==EV_KEY && ev.key==JK_TAB && active)
  378.   { 
  379.     active->draw(0,screen,wm);
  380.     do
  381.     {
  382.       active=active->next;
  383.       if (!active) active=first;
  384.     } while (active && !active->selectable());
  385.     active->draw(1,screen,wm);
  386.   }
  387.   if (active)
  388.   {
  389.     if (ev.type!=EV_MOUSE_MOVE && ev.type!=EV_MOUSE_BUTTON)
  390.       active->handle_event(ev,screen,wm);
  391.     else
  392.     {
  393.       active->area(x1,y1,x2,y2,wm);
  394.       if (ev.mouse_move.x>=x1 && ev.mouse_move.y>=y1 &&
  395.           ev.mouse_move.x<=x2 && ev.mouse_move.y<=y2)
  396.       active->handle_event(ev,screen,wm);
  397.     }
  398.   }
  399.   
  400.   ev.mouse_move.x+=xoff;
  401.   ev.mouse_move.y+=yoff;
  402. }
  403.  
  404. void input_manager::redraw()
  405. {
  406.   ifield *i;
  407.   for (i=first;i;i=i->next)
  408.     i->draw_first(screen,wm);
  409.   if (active)
  410.     active->draw(1,screen,wm);
  411. }
  412.  
  413. input_manager::input_manager(image *Screen, window_manager *WM, ifield *First)
  414. {
  415.   screen=Screen;
  416.   wm=WM;
  417.   active=first=First;
  418.   while (active && !active->selectable()) active=active->next;
  419.   redraw();
  420. }
  421.  
  422. void input_manager::remap(filter *f)
  423. {
  424.   for (ifield *i=first;i;i=i->next)
  425.    i->remap(f);
  426.   redraw();
  427. }
  428.  
  429. void input_manager::add(ifield *i) 
  430. { ifield *f=first;
  431.   if (i->selectable())
  432.   {
  433.     if (!f)
  434.       first=i;
  435.     else
  436.     {
  437.       while (f->next) f=f->next;
  438.       f->next=i; 
  439.     }
  440.   }
  441. }
  442.  
  443. ifield *input_manager::get(int id)
  444. {
  445.   ifield *f;
  446.   for (f=first;f;f=f->next)
  447.   {
  448.     ifield *ret=f->find(id);
  449.     if (ret) return ret;
  450.   }
  451.   return NULL;
  452. }
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.